package com.tinem.platform.service.data.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.common.util.HttpMethod;
import com.tinem.platform.module.pojo.co.GatewayHeadName;
import com.tinem.platform.module.pojo.vo.data.ConstellationInfoRes;
import com.tinem.platform.module.pojo.vo.data.ConstellationInfoVO;
import com.tinem.platform.module.pojo.vo.data.ConstellationVO;
import com.tinem.platform.module.pojo.vo.data.GetProviceVO;
import com.tinem.platform.module.starter.jpa.entity.data.Areas;
import com.tinem.platform.module.starter.jpa.entity.data.Constellation;
import com.tinem.platform.module.starter.jpa.repository.data.ConstellationRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: platform
 * @description: ${description} 星座服务
 * @author: eden#can
 * @Slogan: 没有所谓失败，除非你不再尝试
 * @date: 2021-12-28 13:58
 **/
@Slf4j
@Api(value = "constellation")
@RestController
@RequestMapping("/constellation")
public class ConstellationApi {

    @Resource
    ConstellationRepository constellationRepository;

    @Resource
    HttpServletRequest request;


    @PostMapping("/getConstellations.api")
    @ResponseBody
    @ApiOperation(value = "获取星座列表接口", httpMethod = HttpMethod.POST, notes = "暂无")
    public Object getConstellations(@Validated @RequestBody ConstellationVO req){

        String lang = request.getHeader(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_LANG);

        log.info("获取星座列表接口 getConstellations req json:{}, Lang:{}", JSONUtil.toJsonStr(req),lang);

        List<Constellation> list;
        if(StrUtil.isNotBlank(req.getConstellationId())){
            list = constellationRepository.findByLangIdAndConstellationId(lang, req.getConstellationId());
        } else {
            list = constellationRepository.findByLangId(lang);
        }

        return list;
    }

    @PostMapping("/getConstellationInfo.api")
    @ResponseBody
    @ApiOperation(value = "获取星座列表接口", httpMethod = HttpMethod.POST, notes = "暂无")
    public ConstellationInfoRes getConstellationInfo(@Validated @RequestBody ConstellationInfoVO req){

        log.info("获取星座列表接口 getConstellations req json:{}", JSONUtil.toJsonStr(req));
        List<Constellation> list = constellationRepository.findByLangIdAndConstellationId(req.getLangId(), req.getConstellationId());
        ConstellationInfoRes constellationInfoRes = new ConstellationInfoRes();
        BeanUtil.copyProperties(list.get(0),constellationInfoRes);
        return constellationInfoRes;
    }
}
